package com.yiren.dbaa.module.practice_set.line.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.bean.LinePointBean;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.utils.DateUtil;
import com.yiren.backstage.db.dao.cache.LineInfoCache;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.line.bean.LineMerge;
import com.yiren.dbaa.module.practice_set.line.contract.LineEditContract;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author lwb
 * @date : 2022/6/30 8:55
 * @description:
 */
public class LineEditModel implements LineEditContract.Model {

    private Activity activity;
    LineMerge lineMerge;
    private Gson gson;


    public LineEditModel(Activity activity) {
        this.activity = activity;
        lineMerge = new LineMerge(activity);
        gson = new Gson();
    }

    @Override
    public void getLinePointFromAll(LineInfoEntity lineInfo, CommonCallBack callBack) {
        lineMerge.loadAllLineData(new CommonCallBack() {
            @Override
            public void success(Object result) {
                LineInfoEntity lineInfoBean = LineInfoCache.getLineInfoBean(lineInfo.getId());
                List<LinePointBean> list;
                if (null != lineInfoBean && null != lineInfoBean.getLinePoints()) {
                    list = lineInfoBean.getLinePoints();
                } else {
                    list = new ArrayList<>();
                }
                callBack.success(list);
            }
        });
    }

    @Override
    public void getLinePoint(LineInfoEntity lineInfo, CommonCallBack callBack) {
        lineMerge.loadLineData(lineInfo, new CommonCallBack() {
            @Override
            public void success(Object result) {
                callBack.success(lineInfo.getLinePoints());
            }
        });
    }

    @Override
    public void setLineInfoGps(LineInfoEntity lineInfo) {
        GpsInfoBean gpsInfo = GpsInfoBean.getInstance();
        String address = gpsInfo.getArea() + gpsInfo.getStreet();
        lineInfo.setLocation("nullnull".equals(address) ? "" : address);
        lineInfo.setLongitude(gpsInfo.getLongitude());
        lineInfo.setLatitude(gpsInfo.getLatitude());
    }

    @Override
    public LinePointBean setLinePoint(LineInfoEntity lineInfo, ProjectEntity projectInfo) {
        LinePointBean linePoint = new LinePointBean();
        linePoint.setLinePointId(UUID.randomUUID().toString());
        linePoint.setPointId(projectInfo.getId());
        linePoint.setProjectInfo(projectInfo);
        setLinePointGps(linePoint);
        linePoint.setCreateTm(DateUtil.getCurrentDate());
        return linePoint;
    }

    @Override
    public void setLinePointGps(LinePointBean linePoint) {
        GpsInfoBean gpsInfo = GpsInfoBean.getInstance();
        linePoint.setAltitude(gpsInfo.getAltitude());
        linePoint.setSpeed(gpsInfo.getSpeed());
        linePoint.setBearing(gpsInfo.getBearing());
        linePoint.setLatitude(gpsInfo.getLatitude());
        linePoint.setLongitude(gpsInfo.getLongitude());
        linePoint.setWorkMode(gpsInfo.getWorkMode());
    }

    @Override
    public void setCoachModify(LineInfoEntity lineInfo, List<LinePointBean> tempLinePoints) {
        if (StringUtils.isNotEmpty(lineInfo.getCoachModifyVal())) {
            List<LinePointBean> coachLinePoints = gson.fromJson(lineInfo.getCoachModifyVal(), new TypeToken<List<LinePointBean>>() {
            }.getType());
            if (null != coachLinePoints && coachLinePoints.size() > 0) {
                List<LinePointBean> list = new ArrayList<>();
                for (int i = 0; i < coachLinePoints.size(); i++) {
                    boolean hasPoint = false;
                    for (int j = 0; j < tempLinePoints.size(); j++) {
                        if (tempLinePoints.get(j).getLinePointId().equals(coachLinePoints.get(i).getLinePointId())) {
                            list.add(tempLinePoints.get(j));
                            hasPoint = true;
                            break;
                        }
                    }
                    if (!hasPoint) list.add(coachLinePoints.get(i));
                }
                lineInfo.setCoachModifyVal(gson.toJson(list));
            } else {
                lineInfo.setCoachModifyVal(gson.toJson(tempLinePoints));
            }
        } else {
            lineInfo.setCoachModifyVal(gson.toJson(tempLinePoints));
        }
    }
}
